const fs = require("fs");
const path = require("path");
const inquirer = require("inquirer");
const ejs = require("ejs");

inquirer.prompt([{
    type: "input",
    name: "name",
    message: "你的项目名称：",
    default: "desc",

}]).then(answers => {
    const temp = path.join(__dirname, 'temp');//模板文件目录地址
    const copy = path.join(__dirname, answers.name);//新建文件目录地址
    fs.mkdirSync(copy)

    fs.readdir(temp, (err, dFile) => {  //读文件夹
        if (err) throw err

        dFile.forEach((item, i) => { //遍历文件夹内容
            let index = i;
            ejs.renderFile(path.join(temp, item), answers, (err, data) => { //读取并且渲染模板
                if (err) throw err
                console.log(`${item}项目正在创建`)
                fs.writeFileSync(path.join(copy, item), data) //写入文件
                if (index == dFile.length - 1) {
                    console.log("项目创建成功")
                }
            });

        })
    })
})

